/********************START函数区域*************************/
function totype(value){
	if (value == 1) {
		return '翻译';
	} else if (value == 2) {
		return '校审';
	} else if (value == 3) {
		return '校对';
	} else {
		return '审核';
	};
}

function trim(str,is_global){ 
   var result; 
       result = str.replace(/(^\s+)|(\s+$)/g,""); 
       if(is_global.toLowerCase()=="g")   
       result = result.replace(/\s/g,""); 
       return result; 
}  

/********************函数区域END*************************/

/**================grid显示区域Start========================**/
var cm=new Ext.grid.ColumnModel([
	new Ext.grid.RowNumberer(),
    {header:'稿件标题',width:200,dataIndex:'title',sortable:true},
    {header:'稿件工作类型',width:200,dataIndex:'type',sortable:true,renderer : function(value) {
    	return totype(value);
    }},
    {header:'操作',dataIndex:'id',width:100,
    	renderer :function(value, metadata, record){
    	var title=record.data.title;
    	return "<a href = '#' onclick=javascript:changBoss('" + record.data.manuscriptWorkId +"','" +trim(title,'g')  +"','" + record.data.type +"','" + record.data.securityClassification +"','" + record.data.principalId +"','" + record.data.principalName +"')>变更统稿人 </a> "
    }}
]);

var store=new Ext.data.Store({
	proxy:new Ext.data.HttpProxy({url:'listManuscriptWorkOfPartlyCompelete_list.action',method:'POST'}),
	reader:new Ext.data.JsonReader({
		root:'manuscriptWorksByManuscript',
		totalProperty:'totalCount'},
		[
			{name:'manuscriptId',mapping:'manuscriptId',type:'long'},
			{name:'manuscriptWorkId',mapping:'manuscriptWorkId',type:'long'},
			{name:'type',mapping:'type',type:'long'},
			{name:'status',mapping:'status',type:'long'},
			{name:'principalId',mapping:'principalId'},//统稿人id
			{name:'principalName',mapping:'principalName'},//统稿人姓名
			{name:'confirmerId',mapping:'confirmerId',type:'long'},
			{name:'description',mapping:'description',type:'string'},
			{name:'title',mapping:'manuscript.title',type:'string'},
			{name : 'urgence',mapping : 'manuscript.urgence',type : 'short'}, 
			{name : 'securityClassification',mapping : 'manuscript.securityClassification',type : 'short'},
			{name:'operation',type:'string'}
		]
	)
});

var grid_draw=new Ext.grid.GridPanel({
	title:'变更统稿人管理',
	store:store,
	cm:cm,
	bodyBorder:true,
	region: 'center',
	autoScroll: true,
	autoExpandColumn: 1,
	bbar : new Ext.PagingToolbar({
		pageSize : 20,
		store : store,
		displayInfo : true,
		beforePageText:'第',
		afterPageText:'页-共{0}页',
		displayMsg : '显示{0} - {1} 条  共 {2} 条',
		emptyMsg : "没有记录"
	})
});
store.load({params:{start:0,limit:20}});

var p_draw = {
	    id: module.id,
	    border: false,
	    layout: 'border',
	    items: [grid_draw]
};
Ext.getCmp('content-panel').add(p_draw);
Ext.getCmp('content-panel').layout.setActiveItem(module.id);
/**================grid显示区域End========================**/

/**====================变更通稿人窗口显示区域Start============**/
var principalStore=new Ext.data.JsonStore({
	url:'usersForDistribute_list.action',
	baseParams:{securityClassification:'',workType:''},
	root:'usersForDistribute',
	fields:['userId','userName']
});

window.changBoss=function(manuscriptWorkId,title,type,securityClassification,principalId,principalName){
	//加载可用人员数据源
	principalStore.baseParams.securityClassification=securityClassification;
	principalStore.baseParams.workType=type;
	principalStore.load();
	
	var personForm= new Ext.form.FormPanel({
        border: false,
        labelAlign: 'right',
        labelWidth: 100,
        frame: true,
        items: [{
            fieldLabel: '稿件标题',
            xtype: 'textfield',
            readOnly: true,
            itemCls:'form_text unedit',
            value: title
        },{
        	fieldLabel: '稿件工作类型',
            xtype: 'textfield',
            readOnly: true,
            itemCls:'form_text unedit',
            value: totype(type)
        }, {
			autoHeight : true,
			xtype : 'textfield',
			fieldLabel : '原统稿人',
			readOnly : true,
			value : principalName,
			itemCls:'form_text unedit'
		},{
			id:'person',
			store:principalStore,
			mode:'local',
			xtype:'combo',
			fieldLabel : '人员列表',
			triggerAction : 'all',
			displayField:'userName',
			valueField:'userId',
			itemCls : 'form_text'
		}],
		buttons:[{
			text:'变更',
			type:'submit',
			handler:function(){
			 if(personForm.getForm().isValid()){
				 var newPrincipalId=Ext.getCmp('person').getValue();
				 var newPrincipalName=Ext.getCmp('person').getRawValue();
				 Ext.Ajax.request({
					 url:'changePrincipal_change.action',
					 method:'post',
					 params:{
					 	manuscriptWorkId:manuscriptWorkId,
					 	prePrincipalId:principalId,
					 	newPrincipalId:newPrincipalId,
					 	newPrincipalName:newPrincipalName
				 	 },
				 	success:function(resp, opts){
						 var respText = Ext.util.JSON.decode(resp.responseText);
						 if (respText.success) {
							 Ext.Msg.show({
									title : '信息提示',
									msg : '变更成功',
									buttons : Ext.Msg.OK,
									icon : Ext.Msg.INFO,
									width : 200,
									fn:function(){
								 		showWindow.close();
								 		store.load({params:{start:0,limit:20}});
								 	}
								});
							 store.load({params:{start:0,limit:20}});
						 }else{
							 Ext.Msg.show({
									title : '错误提示',
									msg : respText.errorMessage,
									buttons : Ext.Msg.OK,
									icon : Ext.Msg.ERROR,
									width : 200
								}); 
						 }
					 },
					 failure:function(){
						 Ext.Msg.show({
								title : '错误提示',
								msg : '变更失败!!',
								buttons : Ext.Msg.OK,
								icon : Ext.Msg.ERROR,
								width : 200
							});
					 }
				 });
			 }
			}
		},{
			text:'取消',
			type:'button',
			handler:function(){
				showWindow.close();
			}
		}]
	});
	
	var showWindow=new Ext.Window({//弹出一个显示窗口
		title:'变更统稿人',
		width : 600,
		autoHeight:true,
		modal:true,//True 表示为当window显示时对其后面的一切内容进行遮罩
		resizable :true,//允许用户改变窗口大小
		draggable :true,//表示窗口可以被拖动
		constrain :true,//将窗口约束在视图中
		expandOnShow :true,//窗口显示时总是展开
		items :[personForm]
	}).show();
}
/**====================变更通稿人窗口显示区域End============**/
